<?php
/**
 * Created by PhpStorm.
 * User: 悟能科技
 * Date: 2019/5/18
 * Time: 19:20
 */

namespace app\common\controller;


use think\Controller;

class WxCardDate extends Controller
{

    //会员卡数据处理
    //会员卡创建数据处理   API
    /*
     * $param   主要数据
     *
     */
    public function carduscreate($param){

//        $adata['mid'] = $param['mid'];                                                                                      //商户ID
//        $adata['top_mid'] = $param['top_mid'];
//        $adata['card_id'] = $param['card_id'];                      //卡券ID
        $adata['brand_name'] = $param['brand_name'];
        $adata['get_limit'] = $param['get_limit'];
        $adata['card_type'] = 'MEMBER_CARD';                                                                    //类型
        $adata['logo_url'] = $param['logo_url'];                             //LOGO
        $adata['logo_url_abs'] = $param['logo_url_abs'];                             //LOGO
        $adata['title'] = $param['title'];                                   //名称
        $adata['pic_url'] = $param['pic_url'];                                 //卡面图片url
        $adata['pic_url_abs'] = $param['pic_url_abs'];                                 //卡面图片url
        $adata['bg_url_abs'] = $param['pic_url_abs'];                                 //卡面图片url
        $adata['color'] = $param['color'];                                                           //颜色
        $adata['base_info'] = $param['base_info'];        //卡券基本信息
        $adata['supply_bonus'] = $param['supply_bonus'];
        $adata['is_dedu_cards'] = $param['is_dedu_cards'];
        $adata['supply_balance'] = $param['supply_balance'];
        $adata['center_type'] = $param['is_center'];
        $adata['is_center'] = $param['is_center'];
        $adata['is_cell1'] = $param['is_cell1'];
        $adata['is_cell2'] = $param['is_cell2'];
        $adata['is_cell3'] = $param['is_cell3'];
        $adata['is_cell4'] = $param['is_cell4'];
        $adata['is_money'] = $param['supply_balance'];         //开启余额
//        $adata['is_field1'] = $param['is_field1'];
        $adata['is_field2'] = $param['is_field2'];
        $adata['is_field3'] = $param['is_field3'];
        $adata['is_locations'] = $param['is_locations'];
        //中间按钮
        if ($param['is_center'] == 1){
            if ($param['center'] == 9){
                $adata['center_pay'] = 9;
            }else{
                $adata['center_pay'] = 6;
            }
        }
        //积分
        if ($param['supply_bonus'] == 1) {
            if (isset($param['bonus_rule'])){
                $adata['is_bonus'] = 1;
                $adata['bonus_rule'] = $param['bonus_rule'];
            }
        }
        //上方第一
        if (isset($param['is_field1'])){
            if ($param['is_field1'] == 1) {
                $adata['custom_field1'] = $param['custom_field1'];
            }
        }
        //上方第二
        if (isset($param['is_field2'])) {
            if ($param['is_field2'] == 1) {
                $adata['custom_field2'] = $param['custom_field2'];
            }
        }
        //上方第三
        if (isset($param['is_field3'])) {
            if ($param['is_field3'] == 1) {
                $adata['custom_field3'] =$param['custom_field3'];
            }
        }
        //下方第一
        if (isset($param['is_cell1'])){
            if ($param['is_cell1'] == 1) {
                $adata['custom_cell1'] = $param['custom_cell1'];
            }
        }
        //下方第二
        if (isset($param['is_cell2'])){
            if ($param['is_cell2'] == 1) {
                $adata['custom_cell2'] = $param['custom_cell2'];
            }
        }
        //下方第三
        if (isset($param['is_cell3'])){
            if ($param['is_cell3'] == 1) {
                $adata['custom_cell3'] = $param['custom_cell3'];
            }
        }
        //下方第四
        if (isset($param['is_cell4'])){
            if ($param['is_cell4'] == 1) {
                $adata['custom_cell4'] = $param['custom_cell4'];
            }
        }
        //是否折扣
        if (isset($param['is_discount'])) {
            if ($param['is_discount'] == 1) {
                $adata['discount'] = $param['discount'];                                            //几折
            }
        }
        $adata['prerogative'] = $param['prerogative'];                                                                        //会员卡特权说明,限制1024汉字。
        $adata['statu'] = 0;                                                                                                //状态
        $adata['stocks'] = $param['stocks'];                                    //库存
        $adata['activate'] = $param['activate'];                                                                              //激活： 1/0  自动/一键
        $adata['fire'] = $param['activate'] == 1? 1:2;                                                                         //激活状态  需不需要提交激活字段 1 / 填写信息激活 / 2 自动激活
        $adata['create_time'] = time();
        return $adata;

    }

    //会员卡修改
    /*
   * $arr 主要数据
   * $type 处理类型 接口/存储
   * $info 存储使用
   *
   */
    public function cardusedit($param){
        $adata['brand_name'] = $param['brand_name'];
        $adata['get_limit'] = $param['get_limit'];
        $adata['card_type'] = 'MEMBER_CARD';                                                                    //类型
        $adata['logo_url'] = $param['logo_url'];                             //LOGO
        $adata['logo_url_abs'] = $param['logo_url_abs'];                             //LOGO
        $adata['title'] = $param['title'];                                   //名称
        $adata['pic_url'] = $param['pic_url'];                                 //卡面图片url
        $adata['pic_url_abs'] = $param['pic_url_abs'];                                 //卡面图片url
        $adata['color'] = $param['color'];                                                           //颜色
        $adata['base_info'] =$param['base_info'];        //卡券基本信息
        $adata['supply_bonus'] = $param['supply_bonus'];
        $adata['balance_rules'] = $param['bonus_rule'];
//        $adata['is_dedu_cards'] = $param['is_dedu_cards'];
        $adata['supply_balance'] = $param['supply_balance'];
        $adata['center_type'] = $param['is_center'];
        $adata['is_center'] = $param['is_center'];
        $adata['is_cell1'] = $param['is_cell1'];
        $adata['is_cell2'] = $param['is_cell2'];
        $adata['is_cell3'] = $param['is_cell3'];
        $adata['is_cell4'] = $param['is_cell4'];
        $adata['is_money'] = $param['supply_balance'];         //开启余额
//        $adata['is_field1'] = $param['is_field1'];
        $adata['is_field2'] = $param['is_field2'];
        $adata['is_field3'] = $param['is_field3'];
        $adata['is_locations'] = $param['is_locations'];
        //中间按钮
        if ($param['is_center'] == 1){
            if ($param['center'] == 9){
                $adata['center_pay'] = 9;
            }else{
                $adata['center_pay'] = 6;
            }
        }
        //积分
        if ($param['supply_bonus'] == 1) {
            if (isset($param['bonus_rule'])){
                $adata['is_bonus'] = 1;
                $adata['bonus_rule'] = $param['bonus_rule'];
            }
        }
        //上方第一
        if (isset($param['is_field1'])){
            if ($param['is_field1'] == 1) {
                $adata['custom_field1'] = $param['custom_field1'];
            }
        }
        //上方第二
        if (isset($param['is_field2'])) {
            if ($param['is_field2'] == 1) {
                $adata['custom_field2'] = $param['custom_field2'];
            }
        }
        //上方第三
        if (isset($param['is_field3'])) {
            if ($param['is_field3'] == 1) {
                $adata['custom_field3'] = $param['custom_field3'];
            }
        }
        //下方第一
        if (isset($param['is_cell1'])){
            if ($param['is_cell1'] == 1) {
                $adata['custom_cell1'] = $param['custom_cell1'];
            }
        }
        //下方第二
        if (isset($param['is_cell2'])){
            if ($param['is_cell2'] == 1) {
                $adata['custom_cell2'] = $param['custom_cell2'];
            }
        }
        //下方第三
        if (isset($param['is_cell3'])){
            if ($param['is_cell3'] == 1) {
                $adata['custom_cell3'] = $param['custom_cell3'];
            }
        }
        //下方第四
        if (isset($param['is_cell4'])){
            if ($param['is_cell4'] == 1) {
                $adata['custom_cell4'] = $param['custom_cell4'];
            }
        }
        //是否折扣
        if (isset($param['is_discount'])) {
            if ($param['is_discount'] == 1) {
                $adata['discount'] = $param['discount'];                                            //几折
            }
        }
        $adata['prerogative'] = $param['prerogative'];                                                                        //会员卡特权说明,限制1024汉字。
        $adata['statu'] = 0;                                                                                                //状态
//        $adata['stocks'] = $param['stocks'];                                    //库存
//        $adata['activate'] = $param['activate'];                                                                              //激活： 1/0  自动/一键
//        $adata['fire'] = $param['activate'] == 1? 1:0;                                                                        //激活状态  需不需要提交激活字段
//        $adata['create_time'] = time();
        return $adata;
    }



    //卡券核销
    public function consumecard($arr){
        $data = array(
            'code'=>$arr['code'],
            'card_id'=>$arr['card_id'],

        );
        return $data;
    }



    //创建卡券
    public function cardscreate($arr,$card_type,$type){
        $arr['get_limit'] = $arr['use_limit'];
        $arr['use_custom_code'] = 1;
        if (!empty($arr['funList'])) {
            $arr['can_share'] = isset($arr['funList']['can_share']) ? 1 : 2;
            $arr['is_pay'] = isset($arr['funList']['is_pay']) ? 1 : 2;
            $arr['is_cgive'] = isset($arr['funList']['is_cgive']) ? 1 : 2;
            $arr['can_give_friend'] = isset($arr['funList']['can_give_friend']) ? 1 : 2;
        }else {
            $arr['can_share'] = 2;
            $arr['is_pay'] = 2;
            $arr['is_cgive'] = 2;
            $arr['can_give_friend'] = 2;
        }
        if (!empty($arr['advanced_info']['abstract'])){
            $arr['advanced_info']['abstract']['abstract'] = isset($arr['advanced_info']['abstract']['abstract'])?$arr['advanced_info']['abstract']['abstract']:'';
            $arr['advanced_info']['abstract']['icon_url_list'] = isset($arr['advanced_info']['abstract']['icon_url_list'])?$arr['advanced_info']['abstract']['icon_url_list']:'';
            $arr['advanced_info']['text_image_list'] = isset($arr['advanced_info']['text_image_list'])?$arr['advanced_info']['text_image_list']:[];
        }
        if ($arr['date_info'] == 'DATE_TYPE_FIX_TIME_RANGE') {
            if (isset($arr['times'])) {
                $times =$arr['times'];
                if ($times) {
                    $arr['begin_timestamp'] = $times[0];
                    $arr['end_timestamp'] = $times[1];
                }
            }
        } else {
            $arr['begin_timestamp'] = '';
        }

        switch($card_type) {
            //团购
            case "GROUPON":
                $res = $this->groupon($arr,$type);
                return $res;
                break;
            //代金
            case "CASH":
                $res = $this->sash($arr,$type);
                return $res;
                break;
            //折扣
            case "DISCOUNT":
                $res = $this->discount($arr,$type);
                return $res;
                break;
            //兑换
            case "GIFT":
                $res = $this->gift($arr,$type);
                return $res;
                break;
            //优惠
            case "GENERAL_COUPON":
                $res = $this->coupon($arr,$type);
                return $res;
                break;
        }

    }

    //创建团购券
    public function groupon($arr,$type){
//接口数据
        if ($type == 1){
            $data = array(
                'card'=>array(
                    'card_type'=>'GROUPON',
                    'groupon'=>array(
                        'base_info'=>array(
                            'logo_url'=>$arr['logo_url'],               //卡券logo
                            'brand_name'=>$arr['brand_name'],           //商户名称
                            'code_type'=>"CODE_TYPE_QRCODE",            //非自定义code
                            'title'=>$arr['title'],                     //卡券名称
                            'color'=>$this->getcolor($arr['color']),    //颜色
                            'notice'=>$arr['notice'],                   //使用提醒
                            'service_phone'=>$arr['service_phone'],     //客服电话
                            'description'=>$arr['description'],         //使用说明
                            'date_info'=>array(
                                'type'=>$arr['date_info'],           //使用时间
                            ),
                            'sku'=>array(
                                'quantity'=>$arr['quantity'],       //库存
                            ),
                            'get_limit'=>$arr['get_limit'],             //领取数量
                            'use_limit'=>$arr['use_limit'],             //使用数量
                            'can_share'=>$arr['can_share']==1?true:false,               //分享
                            'can_give_friend'=>$arr['can_give_friend']==1?true:false,   //转赠
                            'use_custom_code'=>$arr['use_custom_code']==1?true:false,   //是否自定义卡号
                        ),
                        'deal_detail'=>$arr['conten'],             //团购券详情

                    ),
                ),
            );
            $data['card']['groupon']['advanced_info'] = !empty($arr['advanced_info'])?$this->advanced_info($arr['advanced_info']):[];        //高级
            if ($arr['date_info'] == 'DATE_TYPE_FIX_TIME_RANGE'){
                $data['card']['groupon']['base_info']['date_info']['begin_timestamp'] = strtotime($arr['begin_timestamp']);
                $data['card']['groupon']['base_info']['date_info']['end_timestamp'] = strtotime($arr['end_timestamp']);
            }else{
                $data['card']['groupon']['base_info']['date_info']['fixed_term'] =$arr['fixed_term'];
            }
        }
        //存储数据
        if ($type == 2){
            $data = array(

                'logo_url' =>$arr['logo_url'],
                'brand_name' =>$arr['brand_name'],
                'times' =>$arr['times'],
                'card_type'=>'GROUPON',
                'title'=>$arr['title'],                    //卡券名称
                'base_info'=>array(
                    'logo_url'=>$arr['logo_url'],               //卡券logo
                    'brand_name'=>$arr['brand_name'],           //商户名称
                    'code_type'=>"CODE_TYPE_QRCODE",            //非自定义code
                    'title'=>$arr['title'],                     //卡券名称
                    'color'=>$arr['color'],                     //颜色
                    'notice'=>$arr['notice'],                   //使用提醒
                    'service_phone'=>$arr['service_phone'],     //客服电话
                    'description'=>$arr['description'],         //使用说明
                    'date_info'=>array(
                        'type'=>$arr['date_info'],           //使用时间

                    ),
                    'sku'=>array(
                        'quantity'=>$arr['quantity'],       //库存
                    ),
                    'get_limit'=>$arr['get_limit'],             //领取数量
                    'use_limit'=>$arr['use_limit'],             //使用数量
                    'can_share'=>$arr['can_share']==1?true:false,               //分享
                    'can_give_friend'=>$arr['can_give_friend']==1?true:false,   //转赠
                    'use_custom_code'=>$arr['use_custom_code']==1?true:false,   //是否自定义卡号

                ),

                'begin_timestamp'=>strtotime($arr['begin_timestamp']),    //生效时间
                'end_timestamp'=>strtotime($arr['end_timestamp']),        //失效时间
                'deal_detail'=>$arr['conten'],             //团购券详情
                'color'=>$this->getcolor($arr['color']),        //背景颜色
                'stocks'=>$arr['quantity'],                      //库存
                'statu'=>0,                                     //状态
//                'is_card_xs'=>$arr['is_card_xs'],               //是否显示
                'create_time'=>time(),                            //创建时间

            );
            if ($arr['date_info'] == 'DATE_TYPE_FIX_TIME_RANGE'){
                $data['base_info']['date_info']['begin_timestamp'] = strtotime($arr['begin_timestamp']);
                $data['base_info']['date_info']['end_timestamp'] = strtotime($arr['end_timestamp']);
            }else{

                $data['end_timestamp'] = strtotime("+1 year");        //失效时间
                $data['base_info']['date_info']['fixed_term'] =$arr['fixed_term'];
                $data['fixed_term'] =$arr['fixed_term'];
            }
            $data['base_info'] = json_encode($data['base_info'],JSON_UNESCAPED_UNICODE);
            $data['advanced_info'] = json_encode($this->advanced_info($arr['advanced_info']),JSON_UNESCAPED_UNICODE);
            $data['get_limit'] = $arr['get_limit'];
            $data['use_limit'] = $arr['use_limit'];

            $data['time_type'] = $arr['date_info'];
            $data['is_pay'] = $arr['is_pay'];
            $data['is_cgive'] = $arr['is_cgive'];

        }
        return $data;
    }

    private function sashdata($arr){
        $data = [
            "swipe_card"=>array(
                "use_mid_list"=>array(
                    $arr['create_mid']

                ),
                "create_mid"=>$arr['create_mid'],
                "is_swipe_card"=>true
            )
        ];

        return $data;
    }

    //高级字段图文处理
    private function advan_imglist($arrs){
        if ($arrs){

            $img = $arrs['image_url'];
            $text = $arrs['text'];
            $data = [];
            if ($img) {
                foreach ($img as $k => $v) {
                    $data[$k]['image_url'] = $v;
                    $data[$k]['text'] = $text[$k];
                }
            }
            return $data;
        }

    }


    //高级字段
    private function advanced_info($arrs){
        $arr['abstract']['abstract'] = !empty($arrs['abstract']['abstract'])?$arrs['abstract']['abstract']:'';
        $arr['abstract']['icon_url_list'] = !empty($arrs['abstract']['icon_url_list'])?[$arrs['abstract']['icon_url_list']]:[];;
        $arr['text_image_list'] =!empty($arrs['text_image_list'])? $this->advan_imglist($arrs['text_image_list']):[];
        return $arr;
    }

    private function sashpass(){
        $data = [
            "center_title"=>"立即使用",
            "center_app_brand_user_name"=> "gh_7195ea80d2e6@app",
            "center_app_brand_pass"=>"pages/index/index",
        ];
    }

    //颜色
    /*
     * 通过实际色值获取接口颜色编号
     *
     */
    public function getcolor($arr){
        //    return $arr;
        switch($arr){
            //浅绿
            case '#63b359':
                $res = 'Color010';
                return $res;
                break;
            //
            case '#2c9f67':
                $res = 'Color020';
                return $res;
                break;
            //
            case '#509fc9':
                $res = 'Color030';
                return $res;
                break;
            //
            case '#5885cf':
                $res = 'Color040';
                return $res;
                break;
            //
            case '#9062c0':
                $res = 'Color050';
                return $res;
                break;
            //
            case '#d09a45':
                $res = 'Color060';
                return $res;
                break;
            //
            case '#e4b138':
                $res = 'Color070';
                return $res;
                break;
            //
            case '#ee903c':
                $res = 'Color080';
                return $res;
                break;
            //
            case '#f08500':
                $res = 'Color081';
                return $res;
                break;
            //
            case '#a9d92d':
                $res = 'Color082';
                return $res;
                break;
            //
            case '#dd6549':
                $res = 'Color090';
                return $res;
                break;
            //
            case '#cc463d':
                $res = 'Color100';
                return $res;
                break;
            //
            case '#cf3e36':
                $res = 'Color101';
                return $res;
                break;
            //
            case '#5E6671':
                $res = 'Color102';
                return $res;
                break;
            default:
                return 'Color030';
        }

    }

    /*
     * 通过接口颜色编号获取实际色值
     *
     */
    public function getencolors($arr){
        //    return $arr;
        switch($arr){
            //浅绿
            case 'Color010':
                $res = '#63b359';
                return $res;
                break;
            //
            case 'Color020':
                $res = '#2c9f67';
                return $res;
                break;
            //
            case 'Color030':
                $res = '#509fc9';
                return $res;
                break;
            //
            case 'Color040':
                $res = '#5885cf';
                return $res;
                break;
            //
            case 'Color050':
                $res = '#9062c0';
                return $res;
                break;
            //
            case 'Color060':
                $res = '#d09a45';
                return $res;
                break;
            //
            case 'Color070':
                $res = '#e4b138';
                return $res;
                break;
            //
            case 'Color080':
                $res = '#ee903c';
                return $res;
                break;
            //
            case 'Color081':
                $res = '#f08500';
                return $res;
                break;
            //
            case 'Color082':
                $res = '#a9d92d';
                return $res;
                break;
            //
            case 'Color090':
                $res = '#dd6549';
                return $res;
                break;
            //
            case 'Color100':
                $res = '#cc463d';
                return $res;
                break;
            //
            case 'Color101':
                $res = '#cf3e36';
                return $res;
                break;
            //
            case 'Color102':
                $res = '#5E6671';
                return $res;
                break;
            default:
                return '#509fc9';
        }
    }

    //代金券
    public function sash($arr,$type){
        if ($type == 1){
            $data = [];
            $data = array(
                'card'=>array(
                    'card_type'=>'CASH',
                    'cash'=>array(
                        'base_info'=>array(
                            'logo_url'=>$arr['logo_url'],               //卡券logo
                            'brand_name'=>$arr['brand_name'],           //商户名称
                            'code_type'=>"CODE_TYPE_QRCODE",            //非自定义code
                            'title'=>$arr['title'],                     //卡券名称
                            'color'=>$this->getcolor($arr['color']),    //颜色

                            'notice'=>$arr['notice'],                   //使用提醒
                            'service_phone'=>$arr['service_phone'],     //客服电话
                            'description'=>$arr['description'],         //使用说明
                            'date_info'=>array(
                                'type'=>$arr['date_info'],           //使用时间

                            ),
                            'sku'=>array(
                                'quantity'=>$arr['quantity'],       //库存
                            ),
                            'get_limit'=>$arr['get_limit'],             //领取数量
                            'use_limit'=>$arr['use_limit'],             //使用数量
                            'can_share'=>$arr['can_share']==1?true:false,               //分享
                            'can_give_friend'=>$arr['can_give_friend']==1?true:false,   //转赠
                            'use_custom_code'=>$arr['use_custom_code']==1?true:false,   //是否自定义卡号

                        ),

                        'least_cost'=>$arr['least_cost']*100,             //起用金额 （单位为分）
                        'reduce_cost'=>$arr['reduce_cost']*100,           //免减金额 （单位为分）
                    ),
                ),
            );

            $data['card']['cash']['advanced_info'] = $this->advanced_info($arr['advanced_info']);
            if ($arr['is_minus'] == 1){
                $data['card']['cash']['base_info']['pay_info'] = $this->sashdata($arr);

            }
            if ($arr['date_info'] == 'DATE_TYPE_FIX_TIME_RANGE'){
                $data['card']['cash']['base_info']['date_info']['begin_timestamp'] = strtotime($arr['begin_timestamp']);
                $data['card']['cash']['base_info']['date_info']['end_timestamp'] = strtotime($arr['end_timestamp']);
            }else{
                $data['card']['cash']['base_info']['date_info']['fixed_term'] =$arr['fixed_term'];
            }

        }
        if ($type == 2){
            $data = array(

                'brand_name' =>$arr['brand_name'],
                'times' =>$arr['times'],
//                'create_mid' =>$arr['create_mid'],
                'logo_url' =>$arr['logo_url'],
                'title'=>$arr['title'],                    //卡券名称
                'card_type'=>'CASH',
                'base_info'=>array(
                    'logo_url'=>$arr['logo_url'],               //卡券logo
                    'brand_name'=>$arr['brand_name'],           //商户名称
                    'code_type'=>"CODE_TYPE_QRCODE",            //非自定义code
//                    'pay_info'=>$this->sashdata($arr),
                    'title'=>$arr['title'],                     //卡券名称
                    'color'=>$arr['color'],                     //颜色
                    'notice'=>$arr['notice'],                   //使用提醒
                    'service_phone'=>$arr['service_phone'],     //客服电话
                    'description'=>$arr['description'],         //使用说明
                    'date_info'=>array(
                        'type'=>$arr['date_info'],           //使用时间

                    ),
                    'sku'=>array(
                        'quantity'=>$arr['quantity'],       //库存
                    ),
                    'get_limit'=>$arr['get_limit'],             //领取数量
                    'use_limit'=>$arr['use_limit'],             //使用数量
                    'can_share'=>$arr['can_share'],               //分享
                    'can_give_friend'=>$arr['can_give_friend'],   //转赠
                    'use_custom_code'=>$arr['use_custom_code']==1?true:false,   //是否自定义卡号
                ),

                'is_minus'=>$arr['is_minus'],
                'begin_timestamp'=>strtotime($arr['begin_timestamp']),    //生效时间
                'end_timestamp'=>strtotime($arr['end_timestamp']),        //失效时间
                'least_cost'=>$arr['least_cost'],             //起用金额 （单位为分）
                'reduce_cost'=>$arr['reduce_cost'],           //免减金额 （单位为分）
                'color'=>$this->getcolor($arr['color']),        //背景颜色
                'stocks'=>$arr['quantity'],                      //库存
                'statu'=>0,                                     //状态
//                'is_card_xs'=>$arr['is_card_xs'],               //是否显示
                'create_time'=>time(),                            //创建时间
            );
            if ($arr['date_info'] == 'DATE_TYPE_FIX_TIME_RANGE'){
                $data['base_info']['date_info']['begin_timestamp'] = strtotime($arr['begin_timestamp']);
                $data['base_info']['date_info']['end_timestamp'] = strtotime($arr['end_timestamp']);
            }else{
//                $end_time = $arr['fixed_term']*86400;
                $data['end_timestamp'] = strtotime("+1 year");        //失效时间
                $data['base_info']['date_info']['fixed_term'] =$arr['fixed_term'];
                $data['fixed_term'] =$arr['fixed_term'];
            }
            $data['base_info'] = json_encode($data['base_info'],JSON_UNESCAPED_UNICODE);
            $data['advanced_info'] = json_encode($this->advanced_info($arr['advanced_info']),JSON_UNESCAPED_UNICODE);
            $data['get_limit'] = $arr['get_limit'];
            $data['use_limit'] = $arr['use_limit'];

            $data['time_type'] = $arr['date_info'];
            $data['is_pay'] = $arr['is_pay'];
            $data['is_cgive'] = $arr['is_cgive'];

        }
        return $data;
    }

//折扣券
    public function discount($arr,$type){
        if ($type == 1){
            $data = array(
                'card'=>array(
                    'card_type'=>'DISCOUNT',
                    'discount'=>array(
                        'base_info'=>array(
                            'logo_url'=>$arr['logo_url'],               //卡券logo
                            'brand_name'=>$arr['brand_name'],           //商户名称
                            'code_type'=>"CODE_TYPE_QRCODE",            //非自定义code
                            'title'=>$arr['title'],                     //卡券名称
                            'color'=>$this->getcolor($arr['color']),    //颜色
                            'notice'=>$arr['notice'],                   //使用提醒
                            'service_phone'=>$arr['service_phone'],     //客服电话
                            'description'=>$arr['description'],         //使用说明
                            'date_info'=>array(
                                'type'=>$arr['date_info'],           //使用时间

                            ),
                            'sku'=>array(
                                'quantity'=>$arr['quantity'],       //库存
                            ),
                            'get_limit'=>$arr['get_limit'],             //领取数量
                            'use_limit'=>$arr['use_limit'],             //使用数量
                            'can_share'=>$arr['can_share']==1?true:false,               //分享
                            'can_give_friend'=>$arr['can_give_friend']==1?true:false,   //转赠
                            'use_custom_code'=>$arr['use_custom_code']==1?true:false,   //是否自定义卡号
                        ),

                        'discount'=>100-$arr['discount'],             //折扣 （30就是7折）
                    ),
                ),
            );
            $data['card']['discount']['advanced_info'] = $this->advanced_info($arr['advanced_info']);
            if ($arr['date_info'] == 'DATE_TYPE_FIX_TIME_RANGE'){
                $data['card']['discount']['base_info']['date_info']['begin_timestamp'] = strtotime($arr['begin_timestamp']);
                $data['card']['discount']['base_info']['date_info']['end_timestamp'] = strtotime($arr['end_timestamp']);
            }else{
                $data['card']['discount']['base_info']['date_info']['fixed_term'] =$arr['fixed_term'];
            }
        }
        if ($type == 2){
            $data = array(

                'brand_name' =>$arr['brand_name'],
                'times' =>$arr['times'],
                'logo_url' =>$arr['logo_url'],
                'title'=>$arr['title'],                    //卡券名称
                'card_type'=>'DISCOUNT',
                'base_info'=>array(
                    'logo_url'=>$arr['logo_url'],               //卡券logo
                    'brand_name'=>$arr['brand_name'],           //商户名称
                    'code_type'=>"CODE_TYPE_QRCODE",            //非自定义code
                    'title'=>$arr['title'],                     //卡券名称
                    'color'=>$arr['color'],                     //颜色
                    'notice'=>$arr['notice'],                   //使用提醒
                    'service_phone'=>$arr['service_phone'],     //客服电话
                    'description'=>$arr['description'],         //使用说明
                    'date_info'=>array(
                        'type'=>$arr['date_info'],           //使用时间

                    ),
                    'sku'=>array(
                        'quantity'=>$arr['quantity'],       //库存
                    ),
                    'get_limit'=>$arr['get_limit'],             //领取数量
                    'use_limit'=>$arr['use_limit'],             //使用数量
                    'can_share'=>$arr['can_share']==1?true:false,               //分享
                    'can_give_friend'=>$arr['can_give_friend']==1?true:false,   //转赠
                    'use_custom_code'=>$arr['use_custom_code']==1?true:false,   //是否自定义卡号
                ),

                'begin_timestamp'=>strtotime($arr['begin_timestamp']),    //生效时间
                'end_timestamp'=>strtotime($arr['end_timestamp']),        //失效时间
                'discount'=>$arr['discount'],             //折扣 （30就是7折）
                'color'=>$this->getcolor($arr['color']),        //背景颜色
                'stocks'=>$arr['quantity'],                      //库存
                'statu'=>0,                                     //状态
//                'is_card_xs'=>$arr['is_card_xs'],               //是否显示
                'create_time'=>time(),                            //创建时间
            );
            if ($arr['date_info'] == 'DATE_TYPE_FIX_TIME_RANGE'){
                $data['base_info']['date_info']['begin_timestamp'] = strtotime($arr['begin_timestamp']);
                $data['base_info']['date_info']['end_timestamp'] = strtotime($arr['end_timestamp']);
            }else{
//                $end_time = $arr['fixed_term']*86400;
                $data['end_timestamp'] = strtotime("+1 year");        //失效时间
                $data['base_info']['date_info']['fixed_term'] =$arr['fixed_term'];
                $data['fixed_term'] =$arr['fixed_term'];
            }
            $data['base_info'] = json_encode($data['base_info'],JSON_UNESCAPED_UNICODE);
            $data['advanced_info'] = json_encode($this->advanced_info($arr['advanced_info']),JSON_UNESCAPED_UNICODE);
            $data['get_limit'] = $arr['get_limit'];
            $data['use_limit'] = $arr['use_limit'];
            $data['time_type'] = $arr['date_info'];
            $data['is_pay'] = $arr['is_pay'];
            $data['is_cgive'] = $arr['is_cgive'];

        }

        return $data;
    }

//兑换券
    public function gift($arr,$type){
        if ($type == 1){
            $data = array(
                'card'=>array(
                    'card_type'=>'GIFT',

                    'gift'=>array(
                        'base_info'=>array(
                            'logo_url'=>$arr['logo_url'],               //卡券logo
                            'brand_name'=>$arr['brand_name'],           //商户名称
                            'code_type'=>"CODE_TYPE_QRCODE",            //非自定义code
                            'title'=>$arr['title'],                     //卡券名称
                            'color'=>$this->getcolor($arr['color']),    //颜色
                            'notice'=>$arr['notice'],                   //使用提醒
                            'service_phone'=>$arr['service_phone'],     //客服电话
                            'description'=>$arr['description'],         //使用说明
                            'date_info'=>array(
                                'type'=>$arr['date_info'],           //使用时间

                            ),
                            'sku'=>array(
                                'quantity'=>$arr['quantity'],       //库存
                            ),
                            'get_limit'=>$arr['get_limit'],             //领取数量
                            'use_limit'=>$arr['use_limit'],             //使用数量
                            'can_share'=>$arr['can_share']==1?true:false,               //分享
                            'can_give_friend'=>$arr['can_give_friend']==1?true:false,   //转赠
                            'use_custom_code'=>$arr['use_custom_code']==1?true:false,   //是否自定义卡号
                            'center_sub_title'=>'使用时请先询问商家',
                        ),

                        'gift'=>$arr['conten'],             //兑换内容
                    ),
                ),
            );
            $data['card']['gift']['advanced_info'] = $this->advanced_info($arr['advanced_info']);
            if ($arr['date_info'] == 'DATE_TYPE_FIX_TIME_RANGE'){
                $data['card']['gift']['base_info']['date_info']['begin_timestamp'] = strtotime($arr['begin_timestamp']);
                $data['card']['gift']['base_info']['date_info']['end_timestamp'] = strtotime($arr['end_timestamp']);
            }else{
                $data['card']['gift']['base_info']['date_info']['fixed_term'] =$arr['fixed_term'];
            }
        }
        if ($type == 2){
            $data = array(

                'brand_name' =>$arr['brand_name'],
                'logo_url' =>$arr['logo_url'],
                'times' =>$arr['times'],
                'title'=>$arr['title'],                    //卡券名称
                'card_type'=>'GIFT',
                'base_info'=>array(
                    'logo_url'=>$arr['logo_url'],               //卡券logo
                    'brand_name'=>$arr['brand_name'],           //商户名称
                    'code_type'=>"CODE_TYPE_QRCODE",            //非自定义code
                    'title'=>$arr['title'],                     //卡券名称
                    'color'=>$arr['color'],                     //颜色
                    'notice'=>$arr['notice'],                   //使用提醒
                    'service_phone'=>$arr['service_phone'],     //客服电话
                    'description'=>$arr['description'],         //使用说明
                    'date_info'=>array(
                        'type'=>$arr['date_info'],           //使用时间

                    ),
                    'sku'=>array(
                        'quantity'=>$arr['quantity'],       //库存
                    ),
                    'get_limit'=>$arr['get_limit'],             //领取数量
                    'use_limit'=>$arr['use_limit'],             //使用数量
                    'can_share'=>$arr['can_share'],               //分享
                    'can_give_friend'=>$arr['can_give_friend'],   //转赠

                ),

                'begin_timestamp'=>strtotime($arr['begin_timestamp']),    //生效时间
                'end_timestamp'=>strtotime($arr['end_timestamp']),        //失效时间
                'gift'=>$arr['conten'],             //兑换内容
                'color'=>$this->getcolor($arr['color']),        //背景颜色
                'stocks'=>$arr['quantity'],                      //库存
                'statu'=>0,                                     //状态
//                'is_card_xs'=>$arr['is_card_xs'],               //是否显示
                'create_time'=>time(),                            //创建时间
            );
            if ($arr['date_info'] == 'DATE_TYPE_FIX_TIME_RANGE'){
                $data['base_info']['date_info']['begin_timestamp'] = strtotime($arr['begin_timestamp']);
                $data['base_info']['date_info']['end_timestamp'] = strtotime($arr['end_timestamp']);
            }else{

                $data['fixed_term'] =$arr['fixed_term'];
                $data['end_timestamp'] = strtotime("+1 year");        //失效时间
                $data['base_info']['date_info']['fixed_term'] =$arr['fixed_term'];
            }
            $data['base_info'] = json_encode($data['base_info'],JSON_UNESCAPED_UNICODE);
            $data['advanced_info'] = json_encode($this->advanced_info($arr['advanced_info']),JSON_UNESCAPED_UNICODE);
            $data['get_limit'] = $arr['get_limit'];
            $data['use_limit'] = $arr['use_limit'];
            $data['time_type'] = $arr['date_info'];
            $data['is_pay'] = $arr['is_pay'];
            $data['is_cgive'] = $arr['is_cgive'];

        }

        return $data;
    }

//优惠券
    public function coupon($arr,$type){
        if ($type == 1){
            $data = array(
                'card'=>array(
                    'card_type'=>'GENERAL_COUPON',
                    'general_coupon'=>array(
                        'base_info'=>array(
                            'logo_url'=>$arr['logo_url'],               //卡券logo
                            'brand_name'=>$arr['brand_name'],           //商户名称
                            'code_type'=>"CODE_TYPE_QRCODE",            //非自定义code
                            'title'=>$arr['title'],                     //卡券名称
                            'color'=>$this->getcolor($arr['color']),    //颜色
                            'notice'=>$arr['notice'],                   //使用提醒
                            'service_phone'=>$arr['service_phone'],     //客服电话
                            'description'=>$arr['description'],         //使用说明
                            'date_info'=>array(
                                'type'=>$arr['date_info'],           //使用时间

                            ),
                            'sku'=>array(
                                'quantity'=>$arr['quantity'],       //库存
                            ),
                            'get_limit'=>$arr['get_limit'],             //领取数量
                            'use_limit'=>$arr['use_limit'],             //使用数量
                            'can_share'=>$arr['can_share']==1?true:false,               //分享
                            'can_give_friend'=>$arr['can_give_friend']==1?true:false,   //转赠
                            'use_custom_code'=>$arr['use_custom_code']==1?true:false,   //是否自定义卡号
                        ),

                        'default_detail'=>$arr['conten'],             //优惠详情
                    ),
                ),
            );
            $data['card']['general_coupon']['advanced_info'] = $this->advanced_info($arr['advanced_info']);
            if ($arr['date_info'] == 'DATE_TYPE_FIX_TIME_RANGE'){
                $data['card']['general_coupon']['base_info']['date_info']['begin_timestamp'] = strtotime($arr['begin_timestamp']);
                $data['card']['general_coupon']['base_info']['date_info']['end_timestamp'] = strtotime($arr['end_timestamp']);
            }else{
                $data['card']['general_coupon']['base_info']['date_info']['fixed_term'] =$arr['fixed_term'];
            }
        }
        if ($type == 2){
            $data = array(

                'logo_url' =>$arr['logo_url'],
                'brand_name' =>$arr['brand_name'],
                'times' =>$arr['times'],
                'title'=>$arr['title'],                    //卡券名称
                'card_type'=>'GENERAL_COUPON',
                'base_info'=>array(
                    'logo_url'=>$arr['logo_url'],               //卡券logo
                    'brand_name'=>$arr['brand_name'],           //商户名称
                    'code_type'=>"CODE_TYPE_QRCODE",            //非自定义code
                    'title'=>$arr['title'],                     //卡券名称
                    'color'=>$arr['color'],                     //颜色
                    'notice'=>$arr['notice'],                   //使用提醒
                    'service_phone'=>$arr['service_phone'],     //客服电话
                    'description'=>$arr['description'],         //使用说明
                    'date_info'=>array(
                        'type'=>$arr['date_info'],           //使用时间

                    ),
                    'sku'=>array(
                        'quantity'=>$arr['quantity'],       //库存
                    ),
                    'get_limit'=>$arr['get_limit'],             //领取数量
                    'use_limit'=>$arr['use_limit'],             //使用数量
                    'can_share'=>$arr['can_share']==1?true:false,               //分享
                    'can_give_friend'=>$arr['can_give_friend']==1?true:false,   //转赠

                ),

                'begin_timestamp'=>strtotime($arr['begin_timestamp']),    //生效时间
                'end_timestamp'=>strtotime($arr['end_timestamp']),        //失效时间
                'default_detail'=>$arr['conten'],             //优惠详情
                'color'=>$this->getcolor($arr['color']),        //背景颜色
                'stocks'=>$arr['quantity'],                      //库存
                'statu'=>0,                                     //状态
//                'is_card_xs'=>$arr['is_card_xs'],               //是否显示
                'create_time'=>time(),                            //创建时间
            );
            if ($arr['date_info'] == 'DATE_TYPE_FIX_TIME_RANGE'){
                $data['base_info']['date_info']['begin_timestamp'] = strtotime($arr['begin_timestamp']);
                $data['base_info']['date_info']['end_timestamp'] = strtotime($arr['end_timestamp']);
            }else{

                $data['end_timestamp'] = strtotime("+1 year");        //失效时间
                $data['base_info']['date_info']['fixed_term'] =$arr['fixed_term'];
                $data['fixed_term'] =$arr['fixed_term'];
            }
            $data['base_info'] = json_encode($data['base_info'],JSON_UNESCAPED_UNICODE);
            $data['advanced_info'] = json_encode($this->advanced_info($arr['advanced_info']),JSON_UNESCAPED_UNICODE);
            $data['get_limit'] = $arr['get_limit'];
            $data['use_limit'] = $arr['use_limit'];
            $data['time_type'] = $arr['date_info'];
            $data['is_pay'] = $arr['is_pay'];
            $data['is_cgive'] = $arr['is_cgive'];

        }

        return $data;
    }

}